<?php
class model_reader extends common_model {
	/**
	 * 基本表属性
	 */
	// protected $table = 'link_hits';
	
	/**
	 * 获取配置
	 */
	public function getConfig() {
		return array(
			array(
				'title' => '凤凰资讯',
				'url' => 'http://news.ifeng.com',
				'expire' => 1200,
				'pattern' => '/<h[12]>\s*<a href="(.*?)" target="_blank" class="sys_url">(.*?)</s',
				'limit' => 10,
				'content' => array(
					'title' => '/<h1 id="artical_topic">(.*?)</s',
					'content' => '/<!--mainContent begin-->(.*?)<!--mainContent end-->/s',
				),
			),
			array(
				'title' => '凤凰科技',
				'url' => 'http://tech.ifeng.com',
				'expire' => 1200,
				'pattern' => '/<div class="fNews">\s*<h3>\s*<a href="(.*?)" target="_blank">(.*?)<\/a>/s',
				'content' => array(
					'title' => '/<h1 id="artical_topic">(.*?)</s',
					'content' => '/<!--mainContent begin-->(.*?)<!--mainContent end-->/s',
				),	
			),
			array(
				'title' => '腾讯科技',
				'url' => 'http://tech.qq.com/',
				'expire' => 2400,
				'pattern' => '/<ul class="news_list2" style="margin-bottom:5px;height:177px;">.*<li><a href="(.*?)" target="_blank">(.*?)<.*?<\/ul/s',
				'charset' => 'gbk',
			),
			array(
				'title' => 'CSDN头条',
				'url' => 'http://www.csdn.net/',
				'pattern' => '/<h1 class="txt_black"><a href="(.*?)".*?">(.*?)</s',
			),
			array(
				'title' => '51CTO头条',
				'url' => 'http://www.51cto.com/',
				'pattern' => '/<H3.*?<A href="(.*?)">(.*?)</s',
				'charset' => 'gbk',
			),
			array(
				'title' => 'ChinaUnix',
				'url' => 'http://www.chinaunix.net/',
				'pattern' => '/<div class="tit3">.*?href="(.*?)".*?>(.*?)</s',
				'charset' => 'gbk',
			),
			array(
				'title' => 'cnBlogs',
				'url' => 'http://www.cnblogs.com/pick/',
				'pattern' => '/<h3><a class="titlelnk" href="(.*?)" target="_blank">(.*?)</s',
				'content' => array(
					'title' => '/<meta name="keywords" content="(.*?)" \/>/s',
					'content' => '/<div id="cnblogs_post_body">(.*?)<\/div>\s+<script/s',
				),
			),
			array(
				'title' => 'InfoQ观点',
				'url' => 'http://www.infoq.com/cn/rss/rss.action?token=lv0DpHJdO1EvcHPjgvrau83MU74xgY1c',
				'pattern' => '/<item.+?<title>(.*?)<.*?<link>(.*?)</s',
				'keys' => array('title', 'url'),
			),
			array(
				'title' => '开源中国',
				'url' => 'http://www.oschina.net',
				'pattern' => '/<h2><a href="(.*?)".*?>(.*?)</s',
			),
			array(
				'title' => 'PHP100',
				'url' => 'http://www.php100.com',
				'pattern' => '/<h1><a href="(.*?)".*?>(.*?)</s',
				'charset' => 'gbk',
			),
			array(
				'title' => 'Yahoo新闻',
				'url' => 'http://rss.news.yahoo.com/rss/topstories',
				'pattern' => '/<item>\s+?<title>(.*?)<.*?<link>(.*?)</s',
				'keys' => array('title', 'url'),
				'limit' => 20,
				'content' => array(
					'content' => '/<div class="yn-story-content">(.*?)<\/div>\s+<div class="yn-share-social">/s',
					'title' => '/<h1 id="yn-title">(.*?)</s',
				),
			),
			array(
				'title' => 'BBC新闻',
				'url' => 'http://feeds.bbci.co.uk/news/rss.xml',
				'pattern' => '/<item>\s+?<title>(.*?)<.*?<link>(.*?)</s',
				'keys' => array('title', 'url'),
				'limit' => 20,
				'content' => array(
					'title' => '/<h1 class="story-header">(.*?)</s',
					'content' => '/<h1 class="story-header">.*?(<p.*?)<\/div><!-- \/ story-body/s',
				),
			),
			array(
				'title' => 'CNET新闻',
				'url' => 'http://news.cnet.com/2547-1_3-0-20.xml?tag=txt',
				'pattern' => '/<item>\s+?<title><!\[CDATA\[(.*?)\]\]><.*?<link>(.*?)</s',
				'keys' => array('title', 'url'),
				'limit' => 20,
				'content' => array(
					'title' => '/<h1>(.*?)</s',
					'content' => '/<div class="postBody" >(.*?)<\/div>\s+<div class="editorBio">/s',
				),
			),
			array(
				'title' => 'cnBeta',
				'url' => 'http://www.cnbeta.com/backend.php',
				'expire' => 1200,
				'pattern' => '/<item>\s+?<title>(.*?)<.*?<link>(.*?)<.*?<category><!\[CDATA\[(.*?)\]\].*?<pubDate>(.*?)</s',
				'keys' => array('title', 'url', 'category', 'time'),
				'limit' => 15,
				'content' => array(
					'title' => '/<h3 id="news_title">(.*?)</s',
					'content' => '/onload="fixPNG\(this\)"\/><\/a>(.*?)<div/s',
					'charset' => 'gbk',
				),
			),
		);
	}
	
	/**
	 * 获取内容
	 */
	public function get($url, $pattern, $expire, $keys, $offset, $limit, $charset) {
		// content
		$content = library_common::get_contents($url, $expire);
		
		if($charset !== '') {
			@$content = iconv($charset, 'utf-8', $content);
		}
		
		// preg
		preg_match_all($pattern, $content, $matches);

		// map
		$result = array();
		
		$count = min(count($matches) - 1, count($keys));
		
		foreach ($matches[1] as $key => $item) {
			if($key >= $offset && $key <= $offset + $limit - 1) {
				for($i = 0; $i < $count; $i++) {
					$temp[$keys[$i]] = trim($matches[$i + 1][$key]);
				}
				$result[] = $temp;
			}
		}
		
		return $result;
	}
	
	/**
	 * 获取文章
	 */
	public function get_item($url, $patterns, $charset) {
		$content = library_common::get_contents($url, 8640000);

		if($charset !== '') {
			@$content = iconv($charset, 'utf-8', $content);
		}

		$result = array();
		foreach($patterns as $key => $value) {
			preg_match($value, $content, $match);
			$result[$key] = isset($match[1]) ? $match[1] : '';
		}

		return $result;
	}

}
?>
